/**
 * 预定顶部搜索
 */


'use strict'
import React, {Component} from 'react';
import {connect} from 'dva';
import {createForm, createFormField} from 'rc-form';
import TagItems from "../../../components/items/TagItems/TagItems";
import PickerItem from "../../../components/items/PickerItem/PickerItem";
import {Button} from "antd-mobile";
import style from './BookClass.less';

// const isIPhone = new RegExp('\\biPhone\\b|\\biPod\\b', 'i').test(window.navigator.userAgent);
// let moneyKeyboardWrapProps;
// if (isIPhone) {
//   moneyKeyboardWrapProps = {
//     onTouchStart: e => e.preventDefault(),
//   };
// }

const opts = ({book}) => {
  const {filterCondition = {}, filterData} = book || {};
  return {filterCondition, filterData};
};

class Condition extends Component {
  state = {
    type: 'money',
  }
  onChange = (value) => {
    console.log(value);
    this.setState({
      value,
    });
  }

  render() {
    const {getFieldProps, getFieldValue} = this.props.form;
    let {
      filterData: {
        sbkc = [],
        szqy = [],
        skfs = [],
        lsxb = [],
        sksj = [],
        jxjy = [],
        bzgm = [],
      } = {},
    } = this.props;
    const fieldValue = getFieldValue('skfs');
    if (fieldValue ==='lssm') {
      bzgm = bzgm.filter(({value}) => value === '1d1');
    }else{
      bzgm = bzgm.filter(({value}) => value !== '1d1');
    }

    return (
      <div>
        <TagItems {...getFieldProps('sbkc')} data={sbkc} >所补课程</TagItems>
        <PickerItem {...getFieldProps('szqy')} data={szqy}>所在区域 </PickerItem>
        <TagItems {...getFieldProps('skfs')} data={skfs}>授课方试</TagItems>
        <TagItems {...getFieldProps('lsxb')} data={lsxb}>老师性别</TagItems>
        <TagItems {...getFieldProps('sksj')} data={sksj} >上课时间</TagItems>
        <TagItems {...getFieldProps('jxjy')} data={jxjy} >教学经验</TagItems>
        <TagItems {...getFieldProps('bzgm')} data={bzgm} >班级规模 </TagItems>
        <div className={style.filterBottom}>
          <Button className={style.filterLeftBtn} onClick={this.reset}>重置</Button>
          <Button className={style.filterRightBtn}
                  onClick={this.onOk}
                  type="primary">确定</Button>
        </div>
      </div>
    );
  }

  reset = _ => this.props.dispatch({type: 'book/filterCondition', payload: {}});

  onOk = _ => {
    const {dispatch, form} = this.props;
    form.validateFields((error, value) => {
      dispatch({type: 'book/bookFilter', body: {filterCondition: value}});
    });


  }
}

Condition.propTypes = {};

const onFieldsChange = (props, fields) => {
  const {dispatch} = props;
  let {filterCondition = {}} = props;
  filterCondition = {...filterCondition, ...fields};
  dispatch({type: 'book/filterCondition', payload: filterCondition});
};

export const mapPropsToFields = (props) => {  //  用于更新页面数据，否则重新赋值刷新不了页面
  const {filterCondition = {}} = props;
  const obj = {};
  const fields = ['sbkc', 'szqy', 'skfs', 'lsxb', 'sksj', 'jxjy', 'bzgm'];
  fields.forEach(field => {
    const fieldValue = Reflect.get(filterCondition, field);
    Reflect.set(obj, field, createFormField(fieldValue));
  });
  return obj;
};

export default connect(opts)(createForm({mapPropsToFields, onFieldsChange})(Condition));
